package com.icss.ah.email.action;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.Json;
import com.icss.ah.login.bean.UserInfoBean;
/**
 * 邮箱认证
 * @author csadchen
 * @date 2016-10-19
 */
@Controller
@Scope("prototype")
public class EmailAuthenticateAction extends BaseAction {
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");

	private String userCode;		//用户编码
	private String emailAddress;	//邮箱地址
	private String opeFlag;			//操作类型
	private Json jsonText;			//返回jsonbean
	/**
	 * 进入邮箱认证页面
	 * @return
	 */
	public String emailAuthenticate(){
		userCode = getUserCodeFromSession();
		emailAddress = emailAuthenticateService.queryUserEmail(userCode);
		return "EmailAuthenticate";
	}
	/*
	 * 发送验证邮件
	 */
	public void sendEmailAuthenticate(){
		jsonText = new Json();
		boolean result = true;
		String message = "";
		try{
			userCode = userCode == null || userCode.equals("null") ? "" : userCode;
			opeFlag = opeFlag == null || opeFlag.equals("null") ? "" : opeFlag;
			emailAddress = emailAddress == null || emailAddress.equals("null") ? "" : emailAddress;
			if(userCode.equals("")){
				result = false;
				message = "用户信息获取失败，请重新登录再进行此操作，谢谢！";
				infolog.info("EmailAuthenticateAction|sendEmail|userCode:"+userCode+"|"+message);
			}
			if(result&&emailAddress.equals("")){
				result = false;
				message = "邮箱地址为空，请重新录入，谢谢！";
				infolog.info("EmailAuthenticateAction|sendEmail|emailAddress:"+emailAddress+"|"+message);
			}
			UserInfoBean userInfo = getUserInfo(userCode);
			if(result){
				String sendResult = emailAuthenticateService.sendEmailAuthenticate(userCode,emailAddress,opeFlag,userInfo,"sendEmailAuthenticate");
				infolog.info("EmailAuthenticateAction|sendEmail|sendEmailAuthenticate|sendResult："+sendResult);
				String[] str = sendResult.split(",");
				if(str[0].equals("1")){
					result = true;
					message = str[1];
					if("您已认证邮箱".equals(str[1])){
						message = "COMPLETED";
					}
				} else if(str[0].equals("0")){
					result = false;
					message = str[1];
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("EmailAuthenticateAction|sendEmail|异常：",e);
		}
		jsonText.setSuccess(result);
		jsonText.setMsg(message);
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}
	
	/*
	 * 发送验证邮件（重置密码）
	 */
	public void sendEmailForResetPwd(){
		jsonText = new Json();
		boolean result = true;
		String message = "";
		try{
			userCode = userCode == null || userCode.equals("null") ? "" : userCode;
			emailAddress = emailAddress == null || emailAddress.equals("null") ? "" : emailAddress;
			if(userCode.equals("")){
				result = false;
				message = "用户信息获取失败，请重新登录再进行此操作，谢谢！";
				infolog.info("EmailAuthenticateAction|sendEmailForResetPwd|userCode:"+userCode+"|"+message);
			}
			if(result&&emailAddress.equals("")){
				result = false;
				message = "邮箱地址为空，请重新录入，谢谢！";
				infolog.info("EmailAuthenticateAction|sendEmailForResetPwd|emailAddress:"+emailAddress+"|"+message);
			}
			UserInfoBean userInfo = getUserInfo(userCode);
			if(result){
				String sendResult = emailAuthenticateService.sendEmailAuthenticate(userCode,emailAddress,"",userInfo,"sendEmailForResetPwd");
				infolog.info("EmailAuthenticateAction|sendEmailForResetPwd|sendResult："+sendResult);
				String[] str = sendResult.split(",");
				if(str[0].equals("1")){
					result = true;
					message = str[1];
				} else if(str[0].equals("0")){
					result = false;
					message = str[1];
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("EmailAuthenticateAction|sendEmailForResetPwd|异常：",e);
		}
		jsonText.setSuccess(result);
		jsonText.setMsg(message);
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}
	
	
	
	/**
	 * 用户邮箱认证
	 * @return
	 */
	public String userAuthenticate(){
		String uuid = getRequest().getParameter("t");
		uuid = uuid == null ? "" : uuid;
		infolog.info("EmailAuthenticateAction|userAuthenticate|uuid:"+uuid);
		String resultInfo = emailAuthenticateService.userAuthenticate(getRequest(),uuid);
		infolog.info("EmailAuthenticateAction|userAuthenticate|resultInfo:"+resultInfo);
		getRequest().setAttribute("resultInfo", resultInfo);
		getRequest().setAttribute("uuid", uuid);
		return "userAuthenticate";
	}
	/**
	 * 重置密码
	 * @return
	 */
	public String doResetPwd(){
		String uuid = getRequest().getParameter("t");
		uuid = uuid == null ? "" : uuid;
		infolog.info("EmailAuthenticateAction|userAuthenticate|uuid:"+uuid);
		String resultInfo = emailAuthenticateService.doResetPwd(getRequest(),uuid);
		infolog.info("EmailAuthenticateAction|userAuthenticate|resultInfo:"+resultInfo);
		getRequest().setAttribute("resultInfo", resultInfo);
		getRequest().setAttribute("uuid", uuid);
		return "toStepTwo";
	}
	
	/**
	 * 邮箱验证结果
	 * @return
	 */
	public String emailAuthenticateResult(){
		String resultInfo = "UN_FIND";
		try{
			userCode = getUserCodeFromSession();
			//获取用户绑定邮箱
			String status = emailAuthenticateService.queryUserEmailAuthenticateStatus(userCode);
//			emailAddress = emailAuthenticateService.queryUserEmail(userCode);
			if(status.equals("1")){
				resultInfo = "COMPLETED";
			} else if(status.equals("0")){
				resultInfo = "WAIT_BIND";
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			getRequest().setAttribute("resultInfo", resultInfo);
		}
		return "emailAuthenticateResult";
	}
	public String getUserCode() {
		return userCode;
	}

	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}
	/**
	 * @return the opeFlag
	 */
	public String getOpeFlag() {
		return opeFlag;
	}
	/**
	 * @param opeFlag the opeFlag to set
	 */
	public void setOpeFlag(String opeFlag) {
		this.opeFlag = opeFlag;
	}
}
